﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;

namespace Ledict.ConditionExtract.DAL
{
  public   class WordBankDAL
    {
           #region DAL 
        private string DbName;
        public WordBankDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_WordBank Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SYS_WordBank(");
            strSql.Append("WB_ID,WB_Name,HeadWordIds,HeadWordNames,WB_CreateTime,WB_UpdateTime)");
            strSql.Append(" values (");
            strSql.Append("@WB_ID,@WB_Name,@HeadWordIds,@HeadWordNames,@WB_CreateTime,@WB_CreateTime)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WB_ID", DbType.String, Model.WB_ID);
            oBase.AddInParameter(oCmd, "@WB_Name", DbType.String, Model.WB_Name);
            oBase.AddInParameter(oCmd, "@HeadWordIds", DbType.String, Model.HeadWordIds);
            oBase.AddInParameter(oCmd, "@HeadWordNames", DbType.String, Model.HeadWordNames);
            oBase.AddInParameter(oCmd, "@WB_CreateTime", DbType.DateTime, Model.WB_CreateTime);
           

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string WB_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_WordBank ");
            strSql.Append(" where WB_ID=@WB_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WB_ID", DbType.String, WB_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        public bool Update(SYS_WordBank Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_WordBank set ");
            strSql.Append("WB_Name=@WB_Name,");
            strSql.Append("HeadWordIds=@HeadWordIds,");
            strSql.Append("HeadWordNames=@HeadWordNames,");
            strSql.Append("WB_UpdateTime=@WB_UpdateTime");
            strSql.Append(" where WB_ID=@WB_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WB_ID", DbType.String, Model.WB_ID);
            oBase.AddInParameter(oCmd, "@WB_Name", DbType.String, Model.WB_Name);
            oBase.AddInParameter(oCmd, "@HeadWordIds", DbType.String, Model.HeadWordIds);
            oBase.AddInParameter(oCmd, "@HeadWordNames", DbType.String, Model.HeadWordNames);
            oBase.AddInParameter(oCmd, "@WB_UpdateTime", DbType.String, Model.WB_UpdateTime);
           

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select row_number()over(order by WB_UpdateTime desc) as showindex , WB_ID,WB_Name,HeadWordIds,HeadWordNames,WB_CreateTime,WB_UpdateTime ");
            strSql.Append(" FROM SYS_WordBank  ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
           

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 通过案由得到相关词库词库
        /// </summary>
        /// <param name="HeadWordId"></param>
        /// <returns></returns>
        public DataTable GetWB_IDByHeadword(string HeadWordId)//or HeadWordIds='all' where ','+HeadWordIds+',' like '%,'+@HeadWordId+',%'
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from dbo.SYS_WordBank  where  HeadWordIds='all' or ','+HeadWordIds+',' like '%,'+@HeadWordId+',%'
            ");

            //SYS_WordBank_KeyWords model = new SYS_WordBank_KeyWords();
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@HeadWordId", DbType.String, HeadWordId);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

     
        #endregion 
    }
}
